package com.woniu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.shop.entity.dto.ListShopAppraiseDTO;
import com.woniu.shop.entity.dto.ShopAppraiseDTO;
import com.woniu.shop.pojo.ShopAppraise;
import com.woniu.shop.mapper.ShopAppraiseMapper;
import com.woniu.shop.pojo.ShopAppraise;
import com.woniu.shop.pojo.vo.ShopAuditVo;
import com.woniu.shop.service.IShopAppraiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 门店评价表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class ShopAppraiseServiceImpl extends ServiceImpl<ShopAppraiseMapper, ShopAppraise> implements IShopAppraiseService {

    @Autowired(required = false)
    private ShopAppraiseMapper shopAppraiseMapper;

    @Autowired
    private IShopAppraiseService shopAppraiseService;

    @Override
    public ShopAuditVo queryAudit(int id) {

        ShopAuditVo shopAuditVo = shopAppraiseMapper.queryAudit(id);

        return shopAuditVo;
    }

    @Override
    public PageResult<ShopAppraise> listShopAppraise(Integer currentPage, Integer pageSize, ListShopAppraiseDTO listShopAppraiseDTO) {
        IPage<ShopAppraise> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<ShopAppraise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(listShopAppraiseDTO.getGarde()), ShopAppraise::getGrade, listShopAppraiseDTO.getGarde());
        wrapper.ge(!StringUtils.isEmpty(listShopAppraiseDTO.getCreatedTimeBegin()), ShopAppraise::getCreatedTime, listShopAppraiseDTO.getCreatedTimeBegin());
        wrapper.le(!StringUtils.isEmpty(listShopAppraiseDTO.getCreatedTimeEnd()), ShopAppraise::getCreatedTime, listShopAppraiseDTO.getCreatedTimeEnd());
        IPage<ShopAppraise> shopAppraisePage = shopAppraiseMapper.selectPage(page, wrapper);
        return new PageResult<>(shopAppraisePage.getTotal(),shopAppraisePage.getRecords());
    }

    @Override
    public boolean reply(Integer id, String replyContent) {
        ShopAppraise shopAppraise = shopAppraiseMapper.selectById(id);
        if(shopAppraise.getReply() != null){
            return false;
        }
        return shopAppraiseMapper.reply(id, replyContent);
    }

    @Override
    public boolean userAppraiseTheShop(ShopAppraiseDTO shopAppraiseDTO) {
        ShopAppraise shopAppraise = new ShopAppraise();
        BeanUtils.copyProperties(shopAppraiseDTO, shopAppraise);
        shopAppraise.setIsDeleted("0"); // 正常
        int row = shopAppraiseMapper.insert(shopAppraise);
        return row != 0;
    }
}
